//
//  CommonDefinition.swift
//  FarmShare
//
//  Created by aykj on 2017/12/22.
//  Copyright © 2017年 aykj. All rights reserved.
//

import Foundation
import UIKit
//import Alamofire



/////////////////////////////////////////////////////////////////////////////////////////////////////////
//majun 隐藏键盘
func hideKeyBoard(){
    UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////

enum refresh:Int{
    case refreshHeader
    case reFreshFooter
}


var get_ColorWithHex: (NSInteger) -> UIColor = {hex in
    return UIColor(red: ((CGFloat)((hex & 0xFF0000) >> 16)) / 255.0, green: ((CGFloat)((hex & 0xFF00) >> 8)) / 255.0, blue: ((CGFloat)(hex & 0xFF)) / 255.0, alpha: 1);
}

/// 根据RGB生成颜色(格式为：22,22,22)
func RGBCOLOR(r:CGFloat, g:CGFloat, b:CGFloat) -> UIColor{
    return UIColor(red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: 1.0)
}
let mainBaseColor = RGBCOLOR(r: 1, g: 186, b: 195)

let mainColor = RGBCOLOR(r: 70, g: 115, b: 235)
let bgGrayColor = RGBCOLOR(r: 242, g: 242, b: 242)
let bordColor = RGBCOLOR(r: 216, g: 216, b: 216)

//功能区字体颜色
let color_f_title = RGBCOLOR(r: 71, g: 72, b: 74)
let color_b_bg = RGBCOLOR(r: 140, g: 194, b: 161)
let color_b_text = RGBCOLOR(r: 98, g: 125, b: 128)

let color_title_gre = RGBCOLOR(r: 141, g: 195, b: 162)

let c_3_g_bg = RGBCOLOR(r: 244, g: 248, b: 251)
let c_3_g_g = RGBCOLOR(r: 140, g: 194, b: 162) //三期绿色

let c_3_title_c = RGBCOLOR(r: 92, g: 126, b: 127) //标题浅绿色
let c_3_title_bg = RGBCOLOR(r: 247, g: 253, b: 250) //标题背景
let c_3_title_bord = RGBCOLOR(r: 215, g: 232, b: 233) //标题边框


let bgImage=UIImage.init(named: "bg_no_image")

let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height



let host = "apptest.ccwb.cn"  //测试
//let host = "nfx360.com"         //正式
let baseURL = "http://\(host)/api/v1/"
let uploadBaseURL = "http://res.ccwb.cn/resource/upload/uploadFile.html"

let clientid = "bmZ4XzRiOWRhYzM1MzRkMDRjZjM="
let secret = "3C8BC2D0E6C340C0CB253DF5EBD35B95"

// 随机字符串生成(MD5加密)
func sid(str:String) -> String{
    let cStr = str.cString(using: String.Encoding.utf8);
    let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
    CC_MD5(cStr!,(CC_LONG)(strlen(cStr!)), buffer)
    let md5String = NSMutableString();
    for i in 0 ..< 16{
        md5String.appendFormat("%02x", buffer[i])
    }
    free(buffer)
    return md5String as String
}

/// 随机字符串生成
class RandomString {
    static let random_str_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    static func getRandomStringOfLength(len : Int) -> String{
        var ranStr = ""
        for _ in 0..<len {
            let index = Int(arc4random_uniform(UInt32(random_str_characters.count)))
            ranStr.append(random_str_characters[random_str_characters.index(random_str_characters.startIndex, offsetBy: index)])
        }
        return ranStr
    }
    
    
    private init() {
        
    }
    static let sharedInstance = RandomString()
}

/************************  屏幕尺寸  ***************************/

// 屏幕宽度

let JMTWindowWidth = UIScreen.main.bounds.size.width

// 屏幕高度

let JMTWindowHeight = UIScreen.main.bounds.size.height

// iPhone4

let isIphone4 = JMTWindowHeight  < 568 ? true : false

// iPhone 5

let isIphone5 = JMTWindowHeight  == 568 ? true : false

// iPhone 6

let isIphone6 = JMTWindowHeight  == 667 ? true : false

// iphone 6P

let isIphone6P = JMTWindowHeight == 736 ? true : false

// iphone X

let isIphoneX = JMTWindowHeight == 812 ? true : false

// navigationBarHeight

let navigationBarHeight : CGFloat = isIphoneX ? 88 : 64

// tabBarHeight

let tabBarHeight : CGFloat = isIphoneX ? 49 + 34 : 49

/************************  屏幕尺寸 end ***************************/

/// 随机字符串生成
//func sid(timeStamp:Int) -> String {
//    var output = ""
//
//    let data = (self as NSString).data(using: String.Encoding.utf8.rawValue)! as NSData
//    return data.MD5().hexedString()

//    for _ in 0..<32 {
//        let randomNumber = arc4random() % 9
////        let randomChar = Character(UnicodeScalar(randomNumber)!)
//        let randomChar = String(randomNumber)
//        output.append(randomChar)
//    }
//    return output
//}




extension String {
    //MARK: - sha1 加密
    func sha1() -> String {
        //UnsafeRawPointer
        let data = self.data(using: String.Encoding.utf8)!
        var digest = [UInt8](repeating: 0, count:Int(CC_SHA1_DIGEST_LENGTH))
        
        let newData = NSData.init(data: data)
        CC_SHA1(newData.bytes, CC_LONG(data.count), &digest)
        let output = NSMutableString(capacity: Int(CC_SHA1_DIGEST_LENGTH))
        for byte in digest {
            output.appendFormat("%02x", byte)
        }
        return output as String
    }
}

func sign(url: String, timeStamp:Int) -> String {

    let encodeUrlString = url.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)
    let eccodetemp = encodeUrlString?.replacingOccurrences(of: "=", with: "%3D")
    
    let code = "\(clientid)timestamp\(timeStamp)sid\(sid(str: String(timeStamp)))url"+eccodetemp!+secret
//    print("签名：\(code)")
    let sign = code.sha1().uppercased()
    
    return sign
}

